package com.iaz.tech.tools.property.impl;

import com.iaz.tech.tools.common.utils.UtilTools;
import com.iaz.tech.tools.property.IPropertyAble;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@EqualsAndHashCode(callSuper = true)
@Data
public class PropertyGroup extends Property{
	/**
     * 日志
     */
	private final static Logger logger = (Logger) LoggerFactory.getLogger(PropertyGroup.class);
	/**
     * 内部树形
     */
    private Map<String, IPropertyAble> properties = new HashMap<String, IPropertyAble>();

    @Override
    public IPropertyAble clone() {
        return null;
    }

    @Override
    public void push(String path, Object _value) {
       try {
    	   if (UtilTools.IsNull(path)) {
    		   throw new Exception("入口参数Path为空。");
    	   }
    	    
    	   String tmp_path = UtilTools.Trim(path); 
    	   
    	   String tmp_filter_paths[] = tmp_path.split(".");
    	   if (tmp_filter_paths == null) {
    		   throw new Exception("path的值为异常。");
    	   }
    	   if (tmp_filter_paths.length == 0 ) {
    		   tmp_filter_paths = new String[1];
    		   tmp_filter_paths[0] = tmp_path;
    	   }
    	   
    	   /* 如果tmp_filter_paths  */
    	   int index_cursor = tmp_filter_paths.length; 
    	   while ( index_cursor < tmp_filter_paths.length ) {
    		   try {
    			   /* 依次比较，如果有则 */
    			   String tmp_path_cursor = UtilTools.Trim(tmp_filter_paths[index_cursor]);
    			   if (UtilTools.IsNull( tmp_path_cursor )) {
    				   throw new Exception("异常，Path列表中，出现空值。");
    			   }
    			   String new_path = "";
				   for(int i = 1; i < tmp_filter_paths.length; i++) {
					   if ( i >= 2) {
						   new_path +=",";
					   }
					   new_path += UtilTools.Trim(tmp_filter_paths[i]);
				   }
    			   
    			   if (this.properties.containsKey(tmp_path_cursor)) {
    				   IPropertyAble _current_property = properties.get(tmp_path_cursor);
    				   _current_property.push(new_path, _value);
    				   continue;
    			   } 
    			   if (index_cursor == tmp_filter_paths.length - 1) {
    				   ValueProperty _current_property = new ValueProperty();
    				   _current_property.push(_value);
    				   _current_property.setPropDataType(_value.getClass());
    				   this.properties.put(tmp_path_cursor, _current_property);
    			   } else {
    				   PropertyGroup propertyGroup = new PropertyGroup();
    				   propertyGroup.push(new_path, _value);
    				   this.properties.put(tmp_path_cursor, propertyGroup);
    			   }
    		   }catch(Exception e2) {
    			   logger.error("异常信息：" + e2.getMessage());
    		   }finally {
    			   index_cursor++;
    		   }
    	   }    	   
       } catch(Exception e) { 
    	   logger.error("执行异常：" + e.getMessage());
       }
    }

	@Override
	public void push(String path, IPropertyAble property) {
		try {
			if (UtilTools.IsNull(path)) {
				throw new Exception("入口参数Path为空。");
			}

			String tmp_path = UtilTools.Trim(path);

			String tmp_filter_paths[] = tmp_path.split(".");
			if (tmp_filter_paths == null) {
				throw new Exception("path的值为异常。");
			}
			if (tmp_filter_paths.length == 0) {
				tmp_filter_paths = new String[1];
				tmp_filter_paths[0] = tmp_path;
			}

			/* 如果tmp_filter_paths */
			int index_cursor = tmp_filter_paths.length;
			while (index_cursor < tmp_filter_paths.length) {
				try {
					/* 依次比较，如果有则 */
					String tmp_path_cursor = UtilTools.Trim(tmp_filter_paths[index_cursor]);
					if (UtilTools.IsNull(tmp_path_cursor)) {
						throw new Exception("异常，Path列表中，出现空值。");
					}
					String new_path = "";
					for (int i = 1; i < tmp_filter_paths.length; i++) {
						if (i >= 2) {
							new_path += ",";
						}
						new_path += UtilTools.Trim(tmp_filter_paths[i]);
					}

					if (properties.containsKey(tmp_path_cursor)) {
						IPropertyAble _current_property = properties.get(tmp_path_cursor);
						_current_property.push(new_path, property);
						continue;
					}
					if (index_cursor == tmp_filter_paths.length - 1) { 
						this.properties.put(tmp_path_cursor, property);
					} else {
						PropertyGroup propertyGroup = new PropertyGroup();
						propertyGroup.push(new_path, property);
						this.properties.put(tmp_path_cursor, propertyGroup);
					}

				} catch (Exception e2) {
					logger.error("异常信息：" + e2.getMessage());
				} finally {
					index_cursor++;
				}
			}
		} catch (Exception e) {
			logger.error("执行异常：" + e.getMessage());
		}
	}

	/**
	 * 根据path 活动区Property
	 * @param path
	 * @return
	 */
	public IPropertyAble getProperty(String path) {
		IPropertyAble result = null;
		try {
			if (UtilTools.IsNull(path)) {
				throw new Exception("入口参数Path为空。");
			}

			String tmp_path = UtilTools.Trim(path);

			String tmp_filter_paths[] = tmp_path.split(".");
			if (tmp_filter_paths == null) {
				throw new Exception("path的值为异常。");
			}
			if (tmp_filter_paths.length == 0) {
				tmp_filter_paths = new String[1];
				tmp_filter_paths[0] = tmp_path;
			}

			if (this.properties == null) {
				throw new Exception("properties 为空！");
			}

			/* 如果tmp_filter_paths */
			List<String> queue = new ArrayList<>();
			for (int i = 0; i < tmp_filter_paths.length; i++) {
				queue.add(tmp_filter_paths[i]);
			}
			IPropertyAble _current_property  = this;
			while(!queue.isEmpty()) {
				String tmp_path_cursor = queue.remove(0);
				if (UtilTools.IsNull(tmp_path_cursor)) {
					continue;
				}
				if ( ((PropertyGroup)_current_property).getProperties()== null) {
					break;
				}
				Map<String, IPropertyAble> map_prop_group = ((PropertyGroup)_current_property).getProperties();
				_current_property = map_prop_group.get(tmp_path_cursor);
				if (queue.isEmpty()) {
					result = _current_property;
					break;
				}
				/* 如果当前Property 为PropertyGroup ，则继续比较下一个Path */
				if (_current_property instanceof PropertyGroup) {
					PropertyGroup _current_property_group = (PropertyGroup) _current_property;
					if (_current_property_group.getProperties() == null) {
						logger.warn("tmp_path_cursor 对应的PropertyGroup 为空！");
						_current_property = null;
						break;
					}
				} else {
					result = null;
					break;
				}
			}
			if (_current_property != null) {
				result = _current_property;
			}
		} catch(Exception e) {
			logger.error("执行异常：" + e.getMessage());
			result = null;
		}
		return result;
	}
	 
}
